﻿using Module.Common.PubEvent;
using Prism.Events;

namespace ServiceItem
{
    public class MovePointHelper
    {
        private readonly IEventAggregator Ea;
        public MovePointHelper(IEventAggregator ea)
        {
            Ea = ea;
            Ea.GetEvent<ServiceMessageEvent>().Subscribe(ReciewMessage);
        }
        /// <summary>
        /// 订阅消息处理
        /// </summary>
        /// <param name="obj">消息实例，类型为Messaage Class</param>
        private void ReciewMessage(ServiceMessage obj)
        {
            switch (obj.MessageName)
            {
                case "getMovePoint":   // 请求计算可移动点消息
                    bool[,] canMovePoint = GetMovePath(obj.Id, obj.QiPanData_Message);
                    Ea.GetEvent<ServiceMessageEvent>().Publish(new ServiceMessage()
                    {
                        MessageName = "movePointOK", //移动路径计算完毕后，发出计算完成消息，附带计算结果
                        MovePoint_Message = canMovePoint,
                        SenderFrom = this.GetType().Name,
                        Content = $"【{this.GetType().Name}】：可移动点计算完毕",
                        ModuleName=obj.ModuleName,
                    });
                    break;
                default: break;
            }
        }
        /// <summary>
        /// 计算棋子可移动的目标位置
        /// </summary>
        /// <param name="moveQiZi">棋子编号</param>
        /// <param name="QipanData">当前棋盘数据</param>
        /// <returns>返回bool二维数组，对应棋盘上的每一点位</returns>
        public static bool[,] GetMovePath(int moveQiZi, in int[,] QipanData)
        {
            bool[,] PathBool = new bool[9, 10];
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    PathBool[i, j] = false;
                }
            }
            #region 从棋盘数据中获取将、帅、指定棋子的行列位置
            int moveQiZiCol = 0;
            int moveQiZiRow = 0;
            int jiangCol = 0;
            int jiangRow = 0;
            int shuaiCol = 0;
            int shuaiRow = 0;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    QipanData[i, j] = QipanData[i, j];
                    if (QipanData[i, j] == 0)
                    {
                        jiangCol = i;
                        jiangRow = j;
                    }
                    if (QipanData[i, j] == 16)
                    {
                        shuaiCol = i;
                        shuaiRow = j;
                    }
                    if (QipanData[i, j] == moveQiZi)
                    {
                        moveQiZiCol = i;
                        moveQiZiRow = j;
                    }
                }
            }
            #endregion

            int side = 0;
            switch (moveQiZi)
            {
                case 7:
                case 8:
                case 23:
                case 24:
                    #region 车的移动 ================================================
                    if (!JustOneIsThis(moveQiZi, jiangCol, jiangRow, shuaiCol, shuaiRow, QipanData))
                    {
                        for (int i = moveQiZiCol - 1; i >= 0; i--) // 同一行向左找
                        {
                            if (QipanData[i, moveQiZiRow] == -1)
                            {
                                PathBool[i, moveQiZiRow] = true;
                            }
                            else
                            {
                                if (!IsTongBang(moveQiZi, QipanData[i, moveQiZiRow]))
                                {
                                    PathBool[i, moveQiZiRow] = true;
                                }
                                break;
                            }
                        }
                        for (int i = moveQiZiCol + 1; i <= 8; i++) // 同一行向右找
                        {
                            if (QipanData[i, moveQiZiRow] == -1)
                            {
                                PathBool[i, moveQiZiRow] = true;
                            }
                            else
                            {
                                if (!IsTongBang(moveQiZi, QipanData[i, moveQiZiRow]))
                                {
                                    PathBool[i, moveQiZiRow] = true;
                                }
                                break;
                            }
                        }
                    }
                    for (int i = moveQiZiRow - 1; i >= 0; i--) // 同一列向上找
                    {
                        if (QipanData[moveQiZiCol, i] == -1)
                        {
                            PathBool[moveQiZiCol, i] = true;
                        }
                        else
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol, i]))
                            {
                                PathBool[moveQiZiCol, i] = true;
                            }
                            break;
                        }
                    }
                    for (int i = moveQiZiRow + 1; i <= 9; i++) // 同一列向下找
                    {
                        if (QipanData[moveQiZiCol, i] == -1)
                        {
                            PathBool[moveQiZiCol, i] = true;
                        }
                        else
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol, i]))
                            {
                                PathBool[moveQiZiCol, i] = true;
                            }
                            break;
                        }
                    }
                    break;
                #endregion
                case 5:
                case 6:
                case 21:
                case 22:
                    #region 马的移动 ================================================
                    if (JustOneIsThis(moveQiZi, jiangCol, jiangRow, shuaiCol, shuaiRow, QipanData))
                    {
                        break;
                    }
                    // 八个方向，逐个检查
                    if (moveQiZiRow > 1 && QipanData[moveQiZiCol, moveQiZiRow - 1] == -1) // 检查上方，如不在边上，且没蹩马腿
                    {
                        if (moveQiZiCol > 0 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 1, moveQiZiRow - 2]))
                        {
                            PathBool[moveQiZiCol - 1, moveQiZiRow - 2] = true;
                        }
                        if (moveQiZiCol < 8 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 1, moveQiZiRow - 2]))
                        {
                            PathBool[moveQiZiCol + 1, moveQiZiRow - 2] = true;
                        }
                    }

                    if (moveQiZiRow < 8 && QipanData[moveQiZiCol, moveQiZiRow + 1] == -1) // 检查下方，如不在边上，且没蹩马腿
                    {
                        if (moveQiZiCol > 0 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 1, moveQiZiRow + 2]))
                        {
                            PathBool[moveQiZiCol - 1, moveQiZiRow + 2] = true;
                        }
                        if (moveQiZiCol < 8 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 1, moveQiZiRow + 2]))
                        {
                            PathBool[moveQiZiCol + 1, moveQiZiRow + 2] = true;
                        }
                    }

                    if (moveQiZiCol > 1 && QipanData[moveQiZiCol - 1, moveQiZiRow] == -1) // 检查左方，如不在边上，且没蹩马腿
                    {
                        if (moveQiZiRow > 0 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 2, moveQiZiRow - 1]))
                        {
                            PathBool[moveQiZiCol - 2, moveQiZiRow - 1] = true;
                        }
                        if (moveQiZiRow < 9 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 2, moveQiZiRow + 1]))
                        {
                            PathBool[moveQiZiCol - 2, moveQiZiRow + 1] = true;
                        }
                    }

                    if (moveQiZiCol < 7 && QipanData[moveQiZiCol + 1, moveQiZiRow] == -1) // 检查右方，如不在边上，且没蹩马腿
                    {
                        if (moveQiZiRow > 0 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 2, moveQiZiRow - 1]))
                        {
                            PathBool[moveQiZiCol + 2, moveQiZiRow - 1] = true;
                        }
                        if (moveQiZiRow < 9 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 2, moveQiZiRow + 1]))
                        {
                            PathBool[moveQiZiCol + 2, moveQiZiRow + 1] = true;
                        }
                    }
                    break;
                #endregion
                case 3:
                case 4:
                case 19:
                case 20:
                    #region 相的移动 ================================================
                    if (moveQiZiRow <= 4) // 如果是上方相，则上下边界设定为0--4，下方相的边界设定为5--9
                    {
                        side = 5;
                    }
                    if (JustOneIsThis(moveQiZi, jiangCol, jiangRow, shuaiCol, shuaiRow, QipanData))
                    {
                        break;
                    }
                    if (moveQiZiRow != 9 - side)  // 如果不在下边界上，则探查下方的可移动路径
                    {
                        if (moveQiZiCol > 0)
                        {
                            if (QipanData[moveQiZiCol - 1, moveQiZiRow + 1] == -1 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 2, moveQiZiRow + 2])) // 左下方
                            {
                                PathBool[moveQiZiCol - 2, moveQiZiRow + 2] = true;
                            }
                        }
                        if (moveQiZiCol < 8)
                        {
                            if (QipanData[moveQiZiCol + 1, moveQiZiRow + 1] == -1 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 2, moveQiZiRow + 2])) // 右下方
                            {
                                PathBool[moveQiZiCol + 2, moveQiZiRow + 2] = true;
                            }
                        }
                    }
                    if (moveQiZiRow != 5 - side)  // 如果不在上边界上，则探查上方的可移动路径
                    {
                        if (moveQiZiCol > 0)
                        {
                            if (QipanData[moveQiZiCol - 1, moveQiZiRow - 1] == -1 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 2, moveQiZiRow - 2])) // 左上方
                            {
                                PathBool[moveQiZiCol - 2, moveQiZiRow - 2] = true;
                            }
                        }
                        if (moveQiZiCol < 8)
                        {
                            if (QipanData[moveQiZiCol + 1, moveQiZiRow - 1] == -1 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 2, moveQiZiRow - 2])) // 右上方
                            {
                                PathBool[moveQiZiCol + 2, moveQiZiRow - 2] = true;
                            }
                        }
                    }
                    break;
                #endregion
                case 1:
                case 2:
                case 17:
                case 18:
                    #region 士的移动 ================================================
                    side = 0;
                    if (moveQiZiRow <= 4) // 如果是上方棋子，则上下边界设定为0--2，下方相的边界设定为7--9
                    {
                        side = 7;
                    }
                    if (JustOneIsThis(moveQiZi, jiangCol, jiangRow, shuaiCol, shuaiRow, QipanData))
                    {
                        break;
                    }
                    if (moveQiZiRow != 9 - side)  // 如果不在下边界上，则探查下方的可移动路径
                    {
                        if (moveQiZiCol > 3)
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol - 1, moveQiZiRow + 1])) // 左下方
                            {
                                PathBool[moveQiZiCol - 1, moveQiZiRow + 1] = true;
                            }
                        }
                        if (moveQiZiCol < 5)
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol + 1, moveQiZiRow + 1])) // 右下方
                            {
                                PathBool[moveQiZiCol + 1, moveQiZiRow + 1] = true;
                            }
                        }
                    }
                    if (moveQiZiRow != 7 - side)  // 如果不在上边界上，则探查上方的可移动路径
                    {
                        if (moveQiZiCol > 3)
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol - 1, moveQiZiRow - 1]))// 左上方
                            {
                                PathBool[moveQiZiCol - 1, moveQiZiRow - 1] = true;
                            }
                        }
                        if (moveQiZiCol < 5)
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol + 1, moveQiZiRow - 1])) // 右上方
                            {
                                PathBool[moveQiZiCol + 1, moveQiZiRow - 1] = true;
                            }
                        }
                    }
                    break;
                #endregion
                case 0:
                case 16:
                    #region 将帅的移动 ================================================
                    side = 0;
                    if (moveQiZiRow <= 4) // 黑将的上下移动边界为0--2，红帅的上下移动边界为7--9
                    {
                        side = 7;
                    }
                    if (moveQiZiRow != (9 - side))  // 如果不在下边界上，则探查下方的可移动路径
                    {
                        if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol, moveQiZiRow + 1])) // 下方移一格
                        {
                            if (jiangCol != shuaiCol)    // 如果将帅横向不同线
                            {
                                PathBool[moveQiZiCol, moveQiZiRow + 1] = true;
                            }
                            else
                            {
                                if (moveQiZi == 0)
                                {
                                    for (int i = jiangRow + 2; i < shuaiRow; i++)
                                    {
                                        if (QipanData[moveQiZiCol, i] != -1)
                                        {
                                            PathBool[moveQiZiCol, moveQiZiRow + 1] = true;
                                            break;
                                        }
                                    }
                                }
                                if (moveQiZi == 16)
                                {
                                    PathBool[moveQiZiCol, moveQiZiRow + 1] = true;
                                }
                            }
                        }
                    }
                    if (moveQiZiRow != (7 - side))  // 如果不在上边界上，则探查上方的可移动路径
                    {
                        if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol, moveQiZiRow - 1]))// 上方移一格
                        {
                            if (jiangCol != shuaiCol)    // 如果将帅横向不同线
                            {
                                PathBool[moveQiZiCol, moveQiZiRow - 1] = true;
                            }
                            else
                            {
                                if (moveQiZi == 0)
                                {
                                    PathBool[moveQiZiCol, moveQiZiRow - 1] = true;
                                }
                                if (moveQiZi == 16)
                                {
                                    for (int i = jiangRow + 1; i < shuaiRow - 1; i++)
                                    {
                                        if (QipanData[moveQiZiCol, i] != -1)
                                        {
                                            PathBool[moveQiZiCol, moveQiZiRow - 1] = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (moveQiZiCol > 3)  // 如果不在左边界上，则探查左方的可移动路径
                    {
                        if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol - 1, moveQiZiRow])) // 左方移一格
                        {
                            if (((moveQiZiCol - 1) == jiangCol) || ((moveQiZiCol - 1) == shuaiCol))    // 如果将帅横向移动一格
                            {
                                for (int i = jiangRow + 1; i < shuaiRow; i++)
                                {
                                    if (QipanData[moveQiZiCol - 1, i] != -1)
                                    {
                                        PathBool[moveQiZiCol - 1, moveQiZiRow] = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                PathBool[moveQiZiCol - 1, moveQiZiRow] = true;
                            }
                        }

                    }
                    if (moveQiZiCol < 5)  // 如果不在右边界上，则探查右方的可移动路径
                    {
                        if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol + 1, moveQiZiRow])) // 右方移一格
                        {
                            if (((moveQiZiCol + 1) == jiangCol) || ((moveQiZiCol + 1) == shuaiCol))    // 如果将帅横向移动一格
                            {
                                for (int i = jiangRow + 1; i < shuaiRow; i++)
                                {
                                    if (QipanData[moveQiZiCol + 1, i] != -1)
                                    {
                                        PathBool[moveQiZiCol + 1, moveQiZiRow] = true;
                                        break;
                                    }
                                }
                            }
                            else
                            {
                                PathBool[moveQiZiCol + 1, moveQiZiRow] = true;
                            }
                        }
                    }
                    for (int i = 0; i <= 8; i++)
                    {
                        for (int j = 0; j <= 9; j++)
                        {
                            if (PathBool[i, j] == true && IsKilledPoint(moveQiZi, i, j, QipanData) == true)
                            {
                                PathBool[i, j] = false;
                            }
                        }
                    }
                    break;
                #endregion
                case 9:
                case 10:
                case 25:
                case 26:
                    #region 炮的移动 ================================================
                    int gezi = 0; // 隔子计数
                    if (!JustOneIsThis(moveQiZi, jiangCol, jiangRow, shuaiCol, shuaiRow, QipanData))
                    {
                        for (int i = moveQiZiCol - 1; i >= 0; i--) // 同一行向左找
                        {
                            if (QipanData[i, moveQiZiRow] == -1)
                            {
                                if (gezi == 0)
                                {
                                    PathBool[i, moveQiZiRow] = true;
                                }
                            }
                            else
                            {
                                if (!IsTongBang(moveQiZi, QipanData[i, moveQiZiRow]))
                                {
                                    if (gezi == 1)
                                    {
                                        PathBool[i, moveQiZiRow] = true;
                                    }
                                }
                                gezi++;
                            }
                        }
                        gezi = 0; // 隔子计数
                        for (int i = moveQiZiCol + 1; i <= 8; i++) // 同一行向右找
                        {
                            if (QipanData[i, moveQiZiRow] == -1)
                            {
                                if (gezi == 0)
                                {
                                    PathBool[i, moveQiZiRow] = true;
                                }
                            }
                            else
                            {
                                if (!IsTongBang(moveQiZi, QipanData[i, moveQiZiRow]))
                                {
                                    if (gezi == 1)
                                    {
                                        PathBool[i, moveQiZiRow] = true;
                                    }
                                }
                                gezi++;
                            }
                        }
                    }
                    gezi = 0; // 隔子计数
                    for (int i = moveQiZiRow - 1; i >= 0; i--) // 同一列向上找
                    {
                        if (QipanData[moveQiZiCol, i] == -1)
                        {
                            if (gezi == 0)
                            {
                                PathBool[moveQiZiCol, i] = true;
                            }
                        }
                        else
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol, i]))
                            {
                                if (gezi == 1)
                                {
                                    PathBool[moveQiZiCol, i] = true;
                                }
                            }
                            gezi++;
                        }
                    }
                    gezi = 0; // 隔子计数
                    for (int i = moveQiZiRow + 1; i <= 9; i++) // 同一列向下找
                    {
                        if (QipanData[moveQiZiCol, i] == -1)
                        {
                            if (gezi == 0)
                            {
                                PathBool[moveQiZiCol, i] = true;
                            }
                        }
                        else
                        {
                            if (!IsTongBang(moveQiZi, QipanData[moveQiZiCol, i]))
                            {
                                if (gezi == 1)
                                {
                                    PathBool[moveQiZiCol, i] = true;
                                }
                            }
                            gezi++;
                        }
                    }
                    break;
                #endregion
                case 11:
                case 12:
                case 13:
                case 14:
                case 15:
                    #region 黑方卒的移动
                    if (moveQiZiRow < 9 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol, moveQiZiRow + 1])) // 下方一格
                    {
                        PathBool[moveQiZiCol, moveQiZiRow + 1] = true;
                    }
                    if (!JustOneIsThis(moveQiZi, jiangCol, jiangRow, shuaiCol, shuaiRow, QipanData) && moveQiZiRow <= 9 && moveQiZiRow > 4) // 水平一格
                    {
                        if (moveQiZiCol > 0 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 1, moveQiZiRow]))
                        {
                            PathBool[moveQiZiCol - 1, moveQiZiRow] = true;
                        }

                        if (moveQiZiCol < 8 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 1, moveQiZiRow]))
                        {
                            PathBool[moveQiZiCol + 1, moveQiZiRow] = true;
                        }
                    }
                    break;
                #endregion
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:
                    #region 红方兵的移动
                    if (moveQiZiRow > 0 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol, moveQiZiRow - 1])) // 上方一格
                    {
                        PathBool[moveQiZiCol, moveQiZiRow - 1] = true;
                    }
                    if (!JustOneIsThis(moveQiZi, jiangCol, jiangRow, shuaiCol, shuaiRow, QipanData) && moveQiZiRow >= 0 && moveQiZiRow < 5) // 水平一格
                    {
                        if (moveQiZiCol > 0 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol - 1, moveQiZiRow]))
                        {
                            PathBool[moveQiZiCol - 1, moveQiZiRow] = true;
                        }

                        if (moveQiZiCol < 8 && !IsTongBang(moveQiZi, QipanData[moveQiZiCol + 1, moveQiZiRow]))
                        {
                            PathBool[moveQiZiCol + 1, moveQiZiRow] = true;
                        }
                    }
                    break;
                #endregion
                default:
                    break;
            }
            return PathBool;
        }

        // 判断是不是同一方棋子
        public static bool IsTongBang(int qz1, int qz2)
        {
            return qz1 >= 0 && qz2 >= 0 && ((qz1 <= 15 && qz2 <= 15) || (qz1 >= 16 && qz2 >= 16));
        }

        /// <summary>
        /// 将帅在同一列时，检查本棋子是否是将帅之间的唯一棋子。
        /// </summary>
        /// <param name="qiZi"></param>
        /// <returns>将帅同列时，如果本棋子是他们之间的唯一棋子，则返回ture。</returns>
        private static bool JustOneIsThis(int qiZi, int jiangCol, int jiangRow, int shuaiCol, int shuaiRow, int[,] QipanData)
        {
            if (jiangCol != shuaiCol)    // 如果将帅不在同一列
            {
                return false;
            }
            bool find = false;
            for (int i = jiangRow + 1; i < shuaiRow; i++)
            {
                if (QipanData[jiangCol, i] == qiZi)
                {
                    find = true; // 棋子在将和帅之间
                    break;
                }
            }
            if (!find) return false;

            int count = 0;
            for (int i = jiangRow + 1; i < shuaiRow; i++)// 棋子在将和帅之间时，查找将帅之间有几个棋子
            {
                if (QipanData[jiangCol, i] != -1)
                {
                    count++;
                }
            }
            return count == 1; // 如果只有一个，则返回true，表示这个棋子不能移开。
        }

        /// <summary>
        /// 在将帅的可移动路径中，排除对方车、马、炮、卒的攻击点。
        /// </summary>
        /// <param name="jiangOrShuai">0=将，16=帅</param>
        /// <param name="col">可移动点的列位置</param>
        /// <param name="row">可移动点的行位置</param>
        /// <param name="QipanData">当前棋盘数据</param>
        /// <returns></returns>
        public static bool IsKilledPoint(int jiangOrShuai, int col, int row, in int[,] qipan)
        {
            // 注意：数组作为参数传递时，不是传递的副本，而是直接数组本身。
            int[,] myqipan = new int[9, 10]; // 制作棋盘副本，防止破坏原棋盘数据数组。
            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 10; j++)
                {
                    myqipan[i, j] = qipan[i, j];
                }
            int jiangCol = -1;
            int jiangRow = -1;
            int shuaiCol = -1;
            int shuaiRow = -1;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    if (myqipan[i, j] == 0) // 找到将所在位置
                    {
                        jiangCol = i;
                        jiangRow = j;
                    }
                    if (myqipan[i, j] == 16) // 找到帅所在位置
                    {
                        shuaiCol = i;
                        shuaiRow = j;
                    }
                }
            }
            if (jiangOrShuai == 0) myqipan[jiangCol, jiangRow] = -1; // 把将或帅先移动到目标位置，再查看对方棋子是否可移动到该点。
            if (jiangOrShuai == 16) myqipan[shuaiCol, shuaiRow] = -1;
            myqipan[col, row] = jiangOrShuai;

            bool[,] thisPathBool;
            if (jiangOrShuai == 16)
            {
                for (int i = 0; i < 9; i++)
                    for (int j = 0; j < 10; j++)
                    {
                        int qizi = myqipan[i, j]; // 从棋盘副本上找棋子
                        if (qizi is >= 5 and <= 15) //车(7,8)，马(5,6)，炮(9,10)，卒(11,12,13,14,15)
                        {
                            thisPathBool = GetMovePath(qizi, myqipan);
                            if (thisPathBool[col, row] == true) return true;
                        }
                    }
            }
            if (jiangOrShuai == 0)
            {
                for (int i = 0; i < 9; i++)
                    for (int j = 0; j < 10; j++)
                    {
                        int qizi = myqipan[i, j]; // 从棋盘副本上找棋子
                        if (qizi is >= 21 and <= 31) //车(23,24)，马(21,22)，炮(25,26)，卒(27,28,29,30,31)
                        {
                            thisPathBool = GetMovePath(qizi, myqipan);
                            if (thisPathBool[col, row] == true) return true;
                        }
                    }
            }
            return false;
        }

        /// <summary>
        /// 棋子移动到指定位置后，是否还是被将军
        /// </summary>
        /// <param name="qiZi">棋子编号</param>
        /// <param name="x1">将要移动的列位置</param>
        /// <param name="y1">将要移动的行位置</param>
        /// <param name="QipanData">当前棋盘数据</param>
        /// <returns></returns>
        public static bool AfterMoveStillJiangJun(int qiZi, int x1, int y1, int[,] qipan)
        {
            int x0 = 0;
            int y0 = 0;
            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 10; j++)
                {
                    if (qipan[i, j] == qiZi)
                    {
                        x0 = i;
                        y0 = j;
                    }
                }
            return AfterMoveStillJiangJun(qiZi, x0, y0, x1, y1, qipan);
        }

        /// <summary>
        /// 检查棋子移动后，本方是否被将军。如果移动后被将军，则通过相关机制不允许该棋子移动到目标位置。
        /// 用于棋子移动前的检测。
        /// </summary>
        /// <param name="qiZi">棋子编号</param>
        /// <param name="x0"></param>
        /// <param name="y0"></param>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="QipanData">棋盘数据</param>
        /// <returns> false=未将军，true=被将军 </returns>
        public static bool AfterMoveStillJiangJun(int qiZi, int x0, int y0, int x1, int y1, in int[,] qipan)
        {
            // 注意：数组作为参数传递时，不是传递参数的副本，而是传递数组本身的地址，是传址而非传参。所以不要直接修改。
            int[,] myqipan = new int[9, 10]; // 制作棋盘副本，防止破坏原棋盘数据数组。
            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 10; j++)
                {
                    myqipan[i, j] = qipan[i, j];
                }
            int jiangCol = 0;
            int jiangRow = 0;
            int shuaiCol = 0;
            int shuaiRow = 0;
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    myqipan[i, j] = myqipan[i, j];
                    if (myqipan[i, j] == 0)
                    {
                        jiangCol = i;
                        jiangRow = j;
                    }
                    if (myqipan[i, j] == 16)
                    {
                        shuaiCol = i;
                        shuaiRow = j;
                    }
                }
            }
            myqipan[x1, y1] = qiZi;
            myqipan[x0, y0] = -1;

            bool[,] thisPathBool;
            for (int i = 0; i < 9; i++)
                for (int j = 0; j < 10; j++)
                {
                    int localQiZi = myqipan[i, j]; // 从棋盘副本上找棋子
                    if (qiZi > 15)
                    {
                        if (localQiZi is >= 5 and <= 15) //车(7,8)，马(5,6)，炮(9,10)，卒(11,12,13,14,15)
                        {
                            thisPathBool = GetMovePath(localQiZi, myqipan);
                            int x = (qiZi == 16) ? x1 : shuaiCol;
                            int y = (qiZi == 16) ? y1 : shuaiRow;
                            if (thisPathBool[x, y] == true) return true;
                        }
                    }
                    if (qiZi < 15)
                    {
                        if (localQiZi is >= 21 and <= 31) //车(23,24)，马(21,22)，炮(25,26)，卒(27,28,29,30,31)
                        {
                            thisPathBool = GetMovePath(localQiZi, myqipan);
                            int x = (qiZi == 0) ? x1 : jiangCol;
                            int y = (qiZi == 0) ? y1 : jiangRow;
                            if (thisPathBool[x, y] == true) return true;
                        }
                    }
                }
            return false; // false=未被将军
        }
        /// <summary>
        /// 残局等棋局设计在自由摆放棋子时，也要对棋子位置是否符合规则进行检查，例如：将帅、象相、仕士、兵卒等，不能超出了活动范围。
        /// </summary>
        /// <param name="qizi"></param>
        /// <param name="col"></param>
        /// <param name="row"></param>
        /// <returns>true=棋子位置合规，false=棋子位置不合规</returns>
        public static bool FreeMovePointHelper(int qizi, int col, int row)
        {
            #region 将帅、象相、仕士、兵卒等的摆放位置，不能超出各自的活动范围
            switch (qizi)
            {
                case 3:
                case 4: // 象 ================================================
                    int[,] pos = new int[7, 2] { { 2, 0 }, { 6, 0 }, { 0, 2 }, { 4, 2 }, { 8, 2 }, { 2, 4 }, { 6, 4 } };
                    for (int i = 0; i < 7; i++)
                    {
                        if (col == pos[i, 0] && row == pos[i, 1]) return true;
                    }
                    break;
                case 19:
                case 20: // 相 ================================================
                    int[,] pos19 = new int[7, 2] { { 2, 0 }, { 6, 0 }, { 0, 2 }, { 4, 2 }, { 8, 2 }, { 2, 4 }, { 6, 4 } };
                    for (int i = 0; i < 7; i++)
                    {
                        if (col == pos19[i, 0] && row == pos19[i, 1] + 5) return true;
                    }
                    break;
                case 1:
                case 2:// 士 ================================================
                    int[,] pos1 = new int[5, 2] { { 3, 0 }, { 5, 0 }, { 4, 1 }, { 3, 2 }, { 5, 2 } };
                    for (int i = 0; i < 5; i++)
                    {
                        if (col == pos1[i, 0] && row == pos1[i, 1]) return true;
                    }

                    break;
                case 17:
                case 18: // 仕 ================================================
                    int[,] pos17 = new int[5, 2] { { 3, 0 }, { 5, 0 }, { 4, 1 }, { 3, 2 }, { 5, 2 } };
                    for (int i = 0; i < 5; i++)
                    {
                        if (col == pos17[i, 0] && row == pos17[i, 1] + 7) return true;
                    }
                    break;
                case 0: // 将 ===============================================
                    if (col >= 3 && col <= 5 && row >= 0 && row <= 2) return true;
                    break;
                case 16: // 帅 ================================================
                    if (col >= 3 && col <= 5 && row >= 7 && row <= 9) return true;
                    break;

                case 11:
                case 12:
                case 13:
                case 14:
                case 15: // 黑方卒
                    if (row < 3) return false;
                    if (row > 4) return true;
                    if (row is 3 or 4)
                    {
                        if (col is 0 or 2 or 4 or 6 or 8) return true;
                    }
                    break;
                case 27:
                case 28:
                case 29:
                case 30:
                case 31:// 红方兵
                    if (row > 6) return false;
                    if (row < 5) return true;
                    if (row is 5 or 6)
                    {
                        if (col is 0 or 2 or 4 or 6 or 8) return true;
                    }
                    break;
                default:
                    return true;
            }
            #endregion
            return false;
        }


    }
}
